#include <iostream>
#include <memory>
#include <vector>
#include <string>
#include <thread>
using namespace std;

#include <Windows.h>

void threadfun(std::string text) {
    Sleep(10);
    cout << text << endl;
}

class sTest1 {
public:
    sTest1() {}
    ~sTest1() {}
    void test(int x) {
        cout << x << ",tid:" << std::this_thread::get_id() << endl;
    }
    void test1(int x1, int x2, int x3) {
        cout << "x1: " << x1;
        cout << ", x2: " << x2;
        cout << ", x3: " << x3 << endl;
    }
};

void test1() {
    auto pt = [](std::thread* td) {
        cout << "joinable:" << (td->joinable() ? "true":"false") << endl;
    };
    sTest1 t1;
    std::unique_ptr<std::thread> td = nullptr;
    td = std::make_unique<std::thread>(
        std::bind(&sTest1::test, &t1, 10));
    pt(td.get());
    td->join();
    pt(td.get());
    
    td = std::make_unique<std::thread>(
        std::bind(&sTest1::test, &t1, 11));
    pt(td.get());
    
    td->join();
    pt(td.get());

    std::thread tdd;
    pt(&tdd);
    
}

void test2() {
    std::thread td;
    cout << td.get_id() << endl;

    sTest1 t1;
    auto fun1 = std::bind(
              &sTest1::test1
            , &t1
            , std::placeholders::_1
            , std::placeholders::_2
            , std::placeholders::_3 );
    std::unique_ptr<std::thread> td1 = std::make_unique<std::thread>(
        fun1, 10 , 20, 30
    );
    td1->join();
}

int main(int argc, char** argv)
{
    cout << "hello world!" << endl;
    
    std::thread td1(threadfun, "td1");
    std::thread td2(threadfun, "td2");
    std::thread td3(threadfun, "td3");
    std::thread td4(threadfun, "td4");
    std::thread td5(threadfun, "td5");
    td1.detach();
    td2.detach();
    td3.detach();
    td4.detach();
    td5.join();
    cout << "main td" << endl;

    cout << "-------test1-------" << endl;
    test1();
    cout << "-------test2-------" << endl;
    test2();

    Sleep(100);
    return 0;
}
